define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/index' + location.search,
                    add_url: 'order/add',
                    edit_url: 'order/edit',
                    del_url: 'order/del',
                    multi_url: 'order/multi',
                    returnprice_url: 'order/retprice',
                    table: 'order',
                },
                searchFormVisible:true,
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', operate:false,title: __('Id')},
                        {field: 'order_number', title: __('Order_numbe')},
                        {field: 'house.h_title',operate:false, title: __('House.h_title')},
                        {field: 'user.nickName', operate:false,title: __('User.nickname')},
                        {field: 'user.phone', operate:false,title: __('User.phone')},
                        {field: 'price',title: __('Price'), operate:false},
                        {field: 'all_price', title: __('All_price'), operate:'BETWEEN'},
                        {field: 'bprice', title: __('Bprice'), operate:false},
                        {field: 'deposit', title: __('Deposit'), operate:false},
                        {field: 'all_money', title: __('All_money'), operate:false},
                        {field: 'income', title:"收入", operate:false},
                        {field: 'pay_time', title: __('Pay_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'pay_status', title: __('Pay_status'),searchList:{"0":__('PayStatus0'),"1":__('PayStatus1'),"2":__('PayStatus2'),"3":__('PayStatus3'),"4":__('PayStatus4'),"5":__('PayStatus5')},formatter:Table.api.formatter.normal},
                        {field: 'refund_status', title: __('Refund_status'),searchList:{"0":__('RefundStatus0'),"1":__('RefundStatus1'),"2":__('RefundStatus2'),"3":__('RefundStatus3'),"4":__('RefundStatus4')},formatter:Table.api.formatter.normal},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}

                    ]
                ]
            });



            // 为表格绑定事件
            Table.api.bindevent(table);
            table.on('post-body.bs.table',function () {

                $(".btn-editone").data("title", "修改订单的信息"); //自定义弹窗标题
                $(".btn-editone").data("area", ["80%","80%"]);//修改弹窗大小
            });

            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据

                //这里我们手动设置底部的值
                $("#c1").text(data.extend.c1);
                $("#c2").text(data.extend.c2);
                $("#c3").text(data.extend.c3);
                $("#c4").text(data.extend.c4);
                $("#c5").text(data.extend.c5);
                $("#c6").text(data.extend.c6);
            });


        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
            $(".btn-refresh").trigger('click');
            //parent.location.reload();//刷新这个页面

        },
        retprice: function () {//没新增一个弹窗页面 这里都要配置，否则 执行成功后 无法关闭弹窗
            Controller.api.bindevent();
            setTimeout(function () {
                $("#c-type").trigger("change");
            }, 100);
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));

            }
        }
    };




    //申请退款
    $("#applyMoney").click(function () {
        var Oids = $("#c-orderIds").val();
        layer.confirm('是否确定申请退款?', {icon: 3, title:'申请退款'}, function(index){

            $.ajax({
                type: "POST",
                url: "order/applyMoney",
                data: {Oids:Oids},
                dataType: "json",
                success: function(data){
                    if(data.code == 1){
                        layer.msg(data.msg,{time:2000,icon: 6})
                        $("#applyMoney").attr("disabled","disabled");
                    }else{
                        layer.msg(data.msg,{time:2000,icon: 5})
                        return false;
                    }

                }
            });

            layer.close(index);
        });

    })


    //执行退款
    $("#doReturnMoney").click(function () {
        var Oids = $("#c-orderIds").val();
        layer.confirm('是否确定执行退款，操作不可回退?', {icon: 3, title:'执行退款'}, function(index){

            $.ajax({
                type: "POST",
                url: "order/doreturnprice",
                data: {Oids:Oids},
                dataType: "json",
                success: function(data){
                    if(data.code == 1){
                        layer.msg(data.msg,{time:2000,icon: 6})
                        $("#doReturnMoney").attr("disabled","disabled");
                    }else{
                        layer.msg(data.msg,{time:2000,icon: 5})
                        return false;
                    }

                }
            });

            layer.close(index);
        });

    })






    return Controller;
});
